package view;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.Kind;

public class KindToevoegen extends javax.swing.JFrame {

    Kind eenKind;
    DateFormat datumformat = new SimpleDateFormat("yyyy-MM-dd");
    
    public KindToevoegen(Kind kindToUpdate) {
        initComponents();
        fieldOudercode.setText(""+kindToUpdate.getOudercode());
        fieldNaam.setText(""+kindToUpdate.getNaam());
        fieldGeboortedatum.setText(""+kindToUpdate.getGeboortedatum());
        
        //vul de boxGeslacht met de geslachten
        boxGeslacht.removeAllItems();
        boxGeslacht.addItem("M");
        boxGeslacht.addItem("V");
        boxGeslacht.setSelectedItem(kindToUpdate.getGeslacht());
        eenKind= kindToUpdate;
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    
    public KindToevoegen(String oudercode) {
        initComponents();
        
        //vul de boxGeslacht met de geslachten
        boxGeslacht.removeAllItems();
        boxGeslacht.addItem("M");
        boxGeslacht.addItem("V");
        boxGeslacht.setSelectedItem("M");
        
        Date vandaag = new Date();
        fieldGeboortedatum.setText(""+datumformat.format(vandaag)); //stel datum van vandaag alvast in als voorbeeld
        
        fieldOudercode.setText(oudercode);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    
    public KindToevoegen() {
        initComponents();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        fieldNaam = new javax.swing.JTextField();
        fieldOudercode = new javax.swing.JTextField();
        fieldGeboortedatum = new javax.swing.JTextField();
        buttonOpslaan = new javax.swing.JButton();
        buttonAnnuleren = new javax.swing.JButton();
        boxGeslacht = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Kind Toevoegen");

        jLabel1.setText("Naam:");

        jLabel2.setText("Geboortedatum:");

        jLabel3.setText("Geslacht:");

        jLabel4.setText("Oudercode:");

        fieldOudercode.setEditable(false);
        fieldOudercode.setBackground(new java.awt.Color(204, 204, 204));

        buttonOpslaan.setText("Opslaan");
        buttonOpslaan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOpslaanActionPerformed(evt);
            }
        });

        buttonAnnuleren.setText("Annuleren");
        buttonAnnuleren.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAnnulerenActionPerformed(evt);
            }
        });

        boxGeslacht.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4))
                                .addGap(25, 25, 25)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(fieldOudercode, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                    .addComponent(fieldNaam, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                    .addComponent(fieldGeboortedatum)
                                    .addComponent(boxGeslacht, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addComponent(buttonOpslaan)
                        .addGap(53, 53, 53)
                        .addComponent(buttonAnnuleren)))
                .addContainerGap(131, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(fieldOudercode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(fieldNaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(fieldGeboortedatum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(boxGeslacht, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonOpslaan)
                    .addComponent(buttonAnnuleren))
                .addContainerGap(110, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonAnnulerenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAnnulerenActionPerformed
        
        new Kindscherm(Integer.parseInt(fieldOudercode.getText())).setVisible(true); //terug naar vorige scherm
        this.dispose();
    }//GEN-LAST:event_buttonAnnulerenActionPerformed

    private void buttonOpslaanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOpslaanActionPerformed
       Kind update = new Kind(); //maak nieuw kind aan om in de database te gaan zetten
       update.setNaam(fieldNaam.getText());
        
       try {
            Date datum = datumformat.parse(fieldGeboortedatum.getText());
            update.setGeboortedatum(datum);
       } catch (ParseException ex) {
            JOptionPane.showMessageDialog(null, "Datum moet ingevoerd worden in de vorm dd-MM-yyyy.", "error", JOptionPane.ERROR_MESSAGE);
       }
        
       update.setGeslacht(""+boxGeslacht.getSelectedItem());
       update.setOudercode(Integer.parseInt(fieldOudercode.getText()));
       
       
       try {update.werkBij(eenKind);} //zet nieuwe gegevens in de database
       catch (Throwable e) {update.voegToe();}
        
       new Kindscherm(Integer.parseInt(fieldOudercode.getText())).setVisible(true); //terug naar vorige scherm
       this.dispose();
    }//GEN-LAST:event_buttonOpslaanActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(KindToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(KindToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(KindToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(KindToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new KindToevoegen().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox boxGeslacht;
    private javax.swing.JButton buttonAnnuleren;
    private javax.swing.JButton buttonOpslaan;
    private javax.swing.JTextField fieldGeboortedatum;
    private javax.swing.JTextField fieldNaam;
    private javax.swing.JTextField fieldOudercode;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables
}
